

	var lazyChart = (function(){
		
		function $(id){
			return document.getElementById(id);
		}
		
		function getScrollTop(){
			return document.documentElement.scrollTop || window.pageYOffset || document.body.scrollTop;
		}
		
		function getDomTop(id){
			return $(id).offsetTop;
		}
		
		function getStyle(element,attr) { 
			if(typeof window.getComputedStyle!='undefined'){
				return parseInt(window.getComputedStyle(element,null)[attr]);           
			}else if(element.currentStyle){
				return parseInt(element.currentStyle[attr]);       
			}
		}
		function cloneObj(oldObj) { //复制对象方法
			if (typeof(oldObj) != 'object') return oldObj;
			if (oldObj == null) return oldObj;
			var newObj = new Object();
			for (var i in oldObj)
				newObj[i] = cloneObj(oldObj[i]);
			return newObj;
		};
		function extendObj() { //扩展对象
			var args = arguments;
			if (args.length < 2) return;
			var temp = cloneObj(args[0]); //调用复制对象方法
			for (var n = 1; n < args.length; n++) {
				for (var i in args[n]) {
					temp[i] = args[n][i];
				}
			}
			return temp;
		}
		
		var eles = document.getElementsByTagName("*");
		var eles_l = eles.length;
		var canvas_dom = [];
		
		function Ld(){}
		Ld.prototype.each = function(callback){
			var l = this.ele.length;
			for(var i=0;i<l;i++){
				var o = {
					ele : this.ele[i],
					index : i
				}
				callback.call(o);
			}
		}
		var ld_dom = new Ld();
		ld_dom.ele = [];
		for(var i=0;i<eles_l;i++){
			if(eles[i].getAttribute("lazydom") == "true"){
				ld_dom.ele.push(eles[i]);
			}
		}
		
		
		function chartInit(echarts){
			ld_dom.each(function(){
				var temp = echarts.init(this.ele);
				temp.isActive = false;
				canvas_dom[this.index] = temp;
			});
		}
		
		function getCharts($index){
			return canvas_dom[$index];
		}
		
		function initSeries(local_option,ajaxdata){  // 初始化series
			var showNum = local_option.extra_kk.showNum || 4;
			if(local_option.series[0].type in {"line":1,"bar":1}){
				var selectedLegend = null;
				for(var m=0;m<local_option.legend.data.length;m++){
					if(local_option.legend.selected[local_option.legend.data[m]] == true){
						selectedLegend = local_option.legend.data[m];
						break;
					}
				}
				for(var m = 0;m<ajaxdata.length;m++){
					local_option.series[m] = local_option.series[0];
					local_option.series[m].data = [];
					var l = ajaxdata[m].Items.length > showNum ? showNum : ajaxdata[m].Items.length;
					local_option.series[m].name = selectedLegend;
					local_option.xAxis[0].data = [];
					for(var i=0;i<l;i++){
						local_option.series[m].data.push(ajaxdata[m].Items[i].Count);
						local_option.xAxis[0].data.push(ajaxdata[0].Items[i].Description); // error 目前只支持一个x轴
					}
				}
			}else if(local_option.series[0].type == "pie"){
				local_option.series[0] = local_option.series[0] || {};
				local_option.series[0].data = [];
				for(var i=0;i<ajaxdata[0].Items.length;i++){
					//local_option.legend.data.push(data.Data.ReportItems[0].Items[i].Description);
					local_option.series[0].data.push({value:ajaxdata[0].Items[i].Count,name:ajaxdata[0].Items[i].Description})
				}
			}
		}
		
		function init(chartConfig){
			ld_dom.each(function(){
				var _top = getDomTop( this.ele.getAttribute("id") );
				var _bottom = _top + getStyle(this.ele,"height");
				if( canvas_dom[this.index].isActive || (!(_top >= getScrollTop() && _top <= getScrollTop() + window.innerHeight ) && !(_bottom >= getScrollTop() && _bottom <= getScrollTop() + window.innerHeight)) ){
					return false;
				}
				var that = this;
				canvas_dom[that.index].isActive = true;
				canvas_dom[that.index].showLoading({text:"loading..."});
				xhr({
					url:chartConfig[that.index].url[0],
					async:true,
					method:'GET',
					success: function  (res) {
						canvas_dom[that.index].hideLoading();
						var data = JSON.parse(res);
						var local_option = chartConfig[that.index].option;
						var showNum = local_option.extra_kk.showNum || 4;
						canvas_dom[that.index].extra_data = {
							index : showNum-1,
							data : data.Data.ReportItems,
							urlArr : chartConfig[that.index].url
						}
						initSeries(local_option,data.Data.ReportItems);
						canvas_dom[that.index].setOption(local_option);
						if("extra_kk" in local_option && "event" in local_option.extra_kk){
							for(var n=0;n<local_option.extra_kk.event.length;n++){
								canvas_dom[that.index].on(local_option.extra_kk.event[n].name,local_option.extra_kk.event[n].handle);
							}
						}
					}
				});
			})
		}
		
		function addData(option){
			for(var i=0;i<canvas_dom[option.index].getOption().series.length;i++){
				var c_data = canvas_dom[option.index].extra_data;
				//var addNum = canvas_dom[option.index].getOption().extra_kk.addNum == 0 ? 0 : (canvas_dom[option.index].getOption().extra_kk.addNum || 4);
				var addNum = 0;
				var showNum = canvas_dom[option.index].getOption().extra_kk.showNum || 4;
				var l = (c_data.data[i].Items.length-showNum) % addNum == 0 ? (c_data.data[i].Items.length-showNum) / addNum : Math.floor((c_data.data[i].Items.length-showNum) / addNum) + 1;
				var add_data = [];
				if(option.dir == "left"){
					var dir = false;
					if(c_data.index >= l*addNum){return false}
					c_data.index = c_data.index+addNum;
					try{
						for(var j=0;j<addNum;j++){
							add_data[j] = [
								i,
								c_data.data[i].Items[c_data.index-j].Count,
								dir,
								false,
								c_data.data[i].Items[c_data.index-j].Description
							];
						}
					}catch(e){}
				}else{
					var dir = true;
					if(c_data.index == 0){
						return false;
					}
					if(c_data.index-addNum < 0){
						c_data.index = 0;
					}else{
						c_data.index = c_data.index-addNum;
					}
					try{
						for(var j=0;j<addNum;j++){
							add_data[j] = [
								i,
								c_data.data[i].Items[c_data.index+j].Count,
								dir,
								false,
								c_data.data[i].Items[c_data.index+j].Description
							];
						}
					}catch(e){}
				}
				add_data = add_data.reverse();
				/*for(var n=0;n<add_data.length;n++){
					console.log(n);
					canvas_dom[option.index].addData([add_data[n]]);
				}*/
				canvas_dom[option.index].addData(add_data);
			}
		}
		
		function toggleLegend($index){
			canvas_dom[$index].on("legendSelected",function(e){
				
				var option = canvas_dom[$index].extra_data;
				
				if(!("ld_select" in option)){
					option.ld_select = canvas_dom[$index].getOption().legend.data[0];
				}
				if(option.ld_select == e.target){
					console.log("点击了当前的ld");
					return false;
				}else{
					console.log("点击了新的ld");
					option.ld_select = e.target;
				}
				var order = null;
				for(var i=0;i<canvas_dom[$index].getOption().legend.data.length;i++){
					if(option.ld_select == canvas_dom[$index].getOption().legend.data[i]){
						order = i;
					}
				}
				xhr({
					url:canvas_dom[$index].extra_data.urlArr[order],
					async:true,
					method:'GET',
					success: function (res) {
						var data = JSON.parse(res);
						canvas_dom[$index].extra_data.data = data.Data.ReportItems;
						var option = canvas_dom[$index].getOption();
						option.legend.selected = {};
						for(var i=0;i<option.legend.data.length;i++){
							option.legend.selected[option.legend.data[i]] = false;
						}
						option.legend.selected[option.legend.data[order]] = true;
						initSeries(option,data.Data.ReportItems);
						canvas_dom[$index].clear();
						canvas_dom[$index].setOption(option);
					}
				})
				return false;
			})
		}
		
		return {
			chartInit : chartInit,
			init : init,
			addData : addData,
			toggleLegend : toggleLegend,
			getCharts : getCharts
		}
		
	})();